/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.core.sql.query;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by walle on 12/9/15.
 */
public class DBRule {
    private String key;
    private Object value;
    private String comparaSign;
    private List<DBRule> rules = new ArrayList();

    public static List<DBRule> addRule(List<DBRule> ruleList, String key, String value, String comString) {
        DBRule cRule = new DBRule(key, value, comString);
        ruleList.add(cRule);
        return ruleList;
    }

    public DBRule addRule(String key, String value, String comString) {
        DBRule cRule = new DBRule(key, value, comString);
        this.rules.add(cRule);
        return this;
    }

    public List<DBRule> getDBRules() {
        return this.rules;
    }

    public static String makeWhereStr(List<DBRule> ruleList) {
        StringBuffer str = new StringBuffer();
        for (DBRule node : ruleList) {
            str.append(node.getThisLimit());
        }
        return str.toString();
    }

    public DBRule(String key, Object value, String comString) {
        DataQuerys.wipeVirus(key);
        DataQuerys.wipeVirus(value.toString());
        DataQuerys.wipeVirus(comString);
        this.key = key.trim().toUpperCase();
        this.value = value;
        this.comparaSign = comString.trim().toUpperCase();
        this.rules.add(this);
    }

    public String getKey() {
        return this.key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return this.value.toString();
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getComparaSign() {
        return this.comparaSign;
    }

    public void setComparaSign(String comparaSign) {
        this.comparaSign = comparaSign;
    }

    public String getThisLimit() {
        StringBuffer where_ = new StringBuffer();
        this.comparaSign = this.comparaSign.toUpperCase();
        if ((this.key != null) && (this.value != null) && (this.comparaSign != null)) {
            where_.append("  AND  ");
            where_.append(this.key);
            if (this.comparaSign.indexOf("LIKE") >= 0) {
                where_.append(" ");
                where_.append("LIKE");
            } else {
                where_.append(" ");
                where_.append(this.comparaSign);
            }
            where_.append(expendVal());
            where_.append(" ");
        }
        return where_.toString();
    }

    private String expendVal() {
        StringBuffer valStr = new StringBuffer();
        if (this.comparaSign.equals("LIKE")) {
            valStr.append(" '%");
            valStr.append(this.value.toString());
            valStr.append("%'");
        } else if (this.comparaSign.equals("-LIKE")) {
            valStr.append(" '%");
            valStr.append(this.value.toString());
            valStr.append("'");
        } else if (this.comparaSign.equals("LIKE-")) {
            valStr.append(" '");
            valStr.append(this.value.toString());
            valStr.append("%'");
        } else if (this.comparaSign.equals("IS NOT")) {
            valStr.append(" ");
            valStr.append(this.value.toString());
            valStr.append(" ");
        } else if ((this.value instanceof String)) {
            valStr.append(" '");
            valStr.append(this.value.toString());
            valStr.append("'");
        } else if ((this.value instanceof BigDecimal)) {
            valStr.append(" ");
            valStr.append(this.value.toString());
            valStr.append("");
        } else {
            valStr.append(" ");
            valStr.append(this.value.toString());
            valStr.append("");
        }

        return valStr.toString();
    }
}
